package com.lebang.service;

import com.lebang.domain.MemberIfMoneyRecord;
import com.lebang.domain.MemberWallet;
import com.lebang.entity.RequestHeaders;
import com.lebang.mapper.MemberIfMoneyRecordMapper;
import com.lebang.service.buss.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2020/12/21 10:28
 * @Version 1.0
 */
@Service
public class MemberIfMoneyService {
    @Autowired
    private MemberIfMoneyRecordMapper memberIfMoneyRecordMapper;
    @Autowired
    private WalletService walletService;
    @Autowired
    private MemberWalletService memberWalletService;

    public void check(RequestHeaders headers) {
        try {
            Integer amounts = walletService.getAllows(headers.getUserid(), headers.getAppid());
            Integer newmy=-1;
            MemberWallet memberWallet = memberWalletService.selectWallet(headers.getUserid());
            if(memberWallet!=null){
                newmy=memberWallet.getMoney().intValue();
            }
            Integer i = memberIfMoneyRecordMapper.selectByMemberId(headers.getUserid());
            if(i==null){
                MemberIfMoneyRecord record = new MemberIfMoneyRecord();
                record.setCreateTime(LocalDateTime.now());
                record.setCreateDate(LocalDate.now());
                record.setOld(amounts);
                record.setMemberId(headers.getUserid());
                record.setNewMy(newmy);
                if (amounts .equals(newmy) ) {
                    record.setType("Success");
                } else {
                    record.setType("Fail");
                }
                memberIfMoneyRecordMapper.insertSelective(record);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
